package com.kongjs.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kongjs.application.convert.OperationLogConverter;
import com.kongjs.application.mapper.OperationLogMapper;
import com.kongjs.application.model.dto.PageDTO;
import com.kongjs.application.model.dto.PageQueryDTO;
import com.kongjs.application.model.dto.QueryOperationLogDTO;
import com.kongjs.application.model.dto.QueryOperationLogParamDTO;
import com.kongjs.application.model.entity.LoginLog;
import com.kongjs.application.model.entity.OperationLog;
import com.kongjs.application.service.IOperationLogService;
import com.kongjs.application.util.PageUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Transactional
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {

    @Resource
    private OperationLogConverter operationLogConverter;

    @Override
    public PageDTO<QueryOperationLogDTO> page(PageQueryDTO dto, QueryOperationLogParamDTO paramDTO) {
        Page<OperationLog> page;
        if (!ObjectUtils.isEmpty(paramDTO)) {
            page = lambdaQuery()
                    .eq(!ObjectUtils.isEmpty(paramDTO.getOperationName()), OperationLog::getOperationName, paramDTO.getOperationName())
                    .eq(!ObjectUtils.isEmpty(paramDTO.getOperationType()), OperationLog::getOperationType, paramDTO.getOperationType())
                    .eq(StringUtils.hasText(paramDTO.getOperationResult()), OperationLog::getOperationResult, paramDTO.getOperationResult())
                    .eq(!ObjectUtils.isEmpty(paramDTO.getUserId()), OperationLog::getUserId, paramDTO.getUserId())
                    .like(StringUtils.hasText(paramDTO.getUserName()), OperationLog::getUserName, paramDTO.getUserName())
                    .like(StringUtils.hasText(paramDTO.getOperationIpaddr()), OperationLog::getOperationIpaddr, paramDTO.getOperationIpaddr())
                    .like(StringUtils.hasText(paramDTO.getOperationIpinfo()),OperationLog::getOperationIpinfo,paramDTO.getOperationIpinfo())
                    .between(!ObjectUtils.isEmpty(paramDTO.getStartDate()) && !ObjectUtils.isEmpty(paramDTO.getEndDate()), OperationLog::getOperationTime, paramDTO.getStartDate(), paramDTO.getEndDate())
                    .orderByDesc(OperationLog::getOperationTime)
                    .page(Page.of(dto.getPageNum(), dto.getPageSize()));
        } else {
            page = lambdaQuery()
                    .orderByDesc(OperationLog::getOperationTime)
                    .page(Page.of(dto.getPageNum(), dto.getPageSize()));
        }
        IPage<QueryOperationLogDTO> iPage = page.convert(operationLog -> operationLogConverter.toDto(operationLog));
        return PageUtil.pageDto(iPage);
    }
}
